<template>
  <el-container class="layout-container">
    <el-aside width="200px" class="layout-aside">
      <div class="logo">
        <h2>ERP-MES平台</h2>
      </div>
      <el-menu
        :default-active="$route.path"
        router
        background-color="#304156"
        text-color="#bfcbd9"
        active-text-color="#409EFF"
      >
        <el-menu-item index="/dashboard">
          <el-icon><Monitor /></el-icon>
          <span>仪表盘</span>
        </el-menu-item>
        
        <el-sub-menu index="erp">
          <template #title>
            <el-icon><Box /></el-icon>
            <span>ERP系统</span>
          </template>
          <el-menu-item index="/erp/inventory">库存管理</el-menu-item>
          <el-menu-item index="/erp/purchase">采购管理</el-menu-item>
          <el-menu-item index="/erp/sales">销售管理</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="mes">
          <template #title>
            <el-icon><Setting /></el-icon>
            <span>MES系统</span>
          </template>
          <el-menu-item index="/mes/workorder">工单管理</el-menu-item>
          <el-menu-item index="/mes/process">工艺管理</el-menu-item>
          <el-menu-item index="/mes/equipment">设备管理</el-menu-item>
          <el-menu-item index="/mes/quality">质量追溯</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="production">
          <template #title>
            <el-icon><Factory /></el-icon>
            <span>生产管理</span>
          </template>
          <el-menu-item index="/production/schedule">生产排产</el-menu-item>
          <el-menu-item index="/production/kanban">现场看板</el-menu-item>
          <el-menu-item index="/production/material">物料拉动</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <el-container>
      <el-header class="layout-header">
        <div class="header-left">
          <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ $route.meta.title || '仪表盘' }}</el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-avatar :size="32" icon="User" />
              <span class="username">{{ userInfo?.name || '管理员' }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main class="layout-main">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const userInfo = computed(() => userStore.userInfo)

const handleLogout = () => {
  userStore.logout()
  ElMessage.success('退出成功')
  router.push('/login')
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.layout-aside {
  background-color: #304156;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  border-bottom: 1px solid #434a50;
}

.logo h2 {
  font-size: 16px;
  font-weight: 600;
}

.layout-header {
  background-color: white;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-right {
  display: flex;
  align-items: center;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin-left: 8px;
}

.layout-main {
  background-color: #f5f5f5;
  padding: 20px;
}
</style>
